package types

import (
	"encoding/json"
	"net/http"
	"sync"
	"web-app/web-server/internal/context"
)

type WebHandler struct {
	Method string
	Path   string
	Func   func(ctx *context.Context, resp http.ResponseWriter, req *http.Request) error
}

type HandlerMapping map[string]*WebHandler

func NewHandlerMapping(WebHandlers []WebHandler) HandlerMapping {
	var mu sync.Mutex
	mu.Lock()
	defer mu.Unlock()
	mapping := make(map[string]*WebHandler)
	for _, wp := range WebHandlers {
		mapping[wp.Path] = &wp
	}
	return mapping
}

func ResultJson(resp http.ResponseWriter, res any) {
	resp.Header().Set("Content-Type", "application/json")
	json, _ := json.Marshal(res)
	resp.Write(json)
}
